//
//  PyobjCMidiPlugIn.m
//  PyobjCMidi
//

#import "PyobjCMidiPlugIn.h"
#import "JSMIDI.h"

// usleep
#import <unistd.h>
/*
@interface PyobjCMidiPlugIn(Private)
@end
*/

@implementation PyobjCMidiPlugIn

#pragma mark _____ plugInLoaded
- (BOOL)plugInLoaded
{
   return YES;
}


#pragma mark _____ superclass overrides
- (id)init
{
   self = [super init];
   if (self) {
      // create ivars
      _statusTextIsEmpty = YES;
      _statusMessageMutex = [[NSLock alloc] init];
      _JSMIDI = [[JSMIDI alloc] initWithController: self];
      NSLog(@"PyobjCMidiPlugIn init");
   }
   return self;
}

- (void)dealloc
{
   NSLog(@"PyobjCMidiPlugIn dealloc");
   NSLog(statusText);
   if (_statusMessageMutex)
   {
      [_statusMessageMutex release];
   }
   
   if (_JSMIDI)
	{
		[_JSMIDI release];
	}
   if (statusText)
   {
      [statusText release];
   }
   [super dealloc];
}


#pragma mark _____ actions
//Constantly appending status message log
- (void)statusMessage:(NSString*)message
{
   NSLog(message);
   
	[_statusMessageMutex lock];   //thread safety
   NSString* status = statusText;
	NSString* newStatus;
	
	// set the new string
	if (_statusTextIsEmpty)
		newStatus = message;
	else
		newStatus = [status stringByAppendingFormat: @"\n\n%@", message];
   
	statusText = newStatus;
	_statusTextIsEmpty = NO;	

	[_statusMessageMutex unlock];	
}


#pragma mark _____ Midi actions

- (void)testMIDI
{
 	[_JSMIDI sendNoteOnChannel: 1 key: 60 velocity: 96];
}

- (void)sendNoteOnChannel:(int)channel key:(int)key velocity:(int)velocity;
{
   NSLog(@"PyobjCMidiPlugIn sendNoteOnChannel");
   
   [_JSMIDI sendNoteOnChannel: channel
                           key: key
                      velocity: velocity];
}

- (void)sendNoteOffChannel:(int)channel key:(int)key velocity:(int)velocity;
{
   NSLog(@"PyobjCMidiPlugIn sendNoteOffChannel");
   
   [_JSMIDI sendNoteOffChannel: channel
                           key: key
                      velocity: velocity];
}

- (void)sendPolyPressureOnChannel:(int)channel key:(int)key pressure:(int)pressure;
{
   NSLog(@"PyobjCMidiPlugIn sendPolyPressureOnChannel");
   
   [_JSMIDI sendPolyPressureOnChannel: channel
                                  key: key
                             pressure: pressure];
}

- (void)sendControlChangeOnChannel:(int)channel index:(int)index value:(int)value;
{
   NSLog(@"PyobjCMidiPlugIn sendControlChangeOnChannel");
   
   [_JSMIDI sendControlChangeOnChannel: channel
                                 index: index
                                 value: value];
}

- (void)sendProgramChangeOnChannel:(int)channel index:(int)index;
{
   NSLog(@"PyobjCMidiPlugIn sendProgramChangeOnChannel");
   
   [_JSMIDI sendProgramChangeOnChannel: channel
                                 index: index];
}

- (void)sendChannelPressureOnChannel:(int)channel pressure:(int)pressure;
{
   NSLog(@"PyobjCMidiPlugIn sendChannelPressureOnChannel");
   
   [_JSMIDI sendChannelPressureOnChannel: channel
                              pressure: pressure];
}

- (void)sendPitchWheelOnChannel:(int)channel value:(int)value;
{
   NSLog(@"PyobjCMidiPlugIn sendPitchWheelOnChannel");
   
   [_JSMIDI sendPitchWheelOnChannel: channel
                                 value: value];
}


@end

